const app =getApp()
import { request } from '../../../../utils/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 自定义nav
    statusBarHeight1: app.globalData.statusBarHeight+45,
    // 切换兰变量
    active: 0,
    // 一级目录
    first: '',
    // 二级目录
    second: '',
    
    // 书籍详情
    bookDetail: {},
    // 图片基础路径
    imgBaseUrl: app.globalData.imgBaseUrl,
    // 目录列表
    muLuList: [],
    bookId: '',
    // 二级目录列表
    secondList: [],
    // 三级目录列表
    thirdList: [],
    // 是否隐藏二级目录
    hideSecond: false,
    // 是都隐藏三级目录
    hideThird: false,
    // 当前点击的一级目录
    clickOne: 0,
    // 当前点击的二级目录
    clickTwo: 0,
    // 评论列表
    pingLunList: [],
    // 是否显示播放视频弹窗
    showVideo: false,
    // 视频连接
    videoUrl: '',
    isXi: false,
    scrollHeight: '',
    style: 'background-color: pink;width: 100vw;height: 100rpx;position: fixed;top: 60rpx;left: 0;z-index: 9;'

  },
  // 返回
  goBack() {
    wx.navigateBack({
      delta: 1
    })
  },
  // 获取滚动条的位置
  onPageScroll(e) {
    console.log(e);
    if(e.scrollTop >= this.data.scrollHeight) {
      this.setData({
        isXi: true
      })
    } else {
      this.setData({
        isXi: false
      })
    }
  },

  // 吸顶的tab 点击事件
  clickXiTab(e) {
    console.log(e);
    this.setData({
      active: e.currentTarget.dataset.type
    })
    console.log(this.selectComponent('#tabs'));
  },
  // 切换兰事件
  onChange(event) {
    // wx.showToast({
    //   title: `切换到标签 ${event.detail.name}`,
    //   icon: 'none',
    // });
  },
  // 一级目录切换事件
  onChange1(e) {
    console.log(e);
    this.setData({
      first: e.detail,
    });
    this.getSecond(e.detail)
  },
  // 二级目录 切换事件
  onSecond(event) {
    this.setData({
      second: event.detail,
    });
  },
  // ====
  // 一级目录点击事件
  onFirst(e) {
    console.log(e);
    let mlId = e.currentTarget.dataset.mlid
    if(this.data.clickOne != mlId || this.data.clickOne == 0) {
      // this.getSecond(mlId)
      
      request({
        url: '/zhongyisheji-applet/lookBook/public/bookPlanList',
        method: 'post',
        data: {
          pid: mlId,
          pageNum: '1',
          pageSize: '99',
          bookId: this.data.bookId
        }
      }).then(res => {
        console.log('二级目录',res);
        if(res.code == 200) {
          if(res.data.records.length == 0) {
            wx.showToast({
              title: '暂无子目录！',
              icon: 'none'
            })
          }
          this.setData({
            secondList: res.data.records
          })

          this.setData({
            clickOne: mlId,
            clickTwo: 0
          })
          // if(this.data.clickOne == 0) {
          //   this.setData({
          //     clickOne: mlId
          //   })
          // } else if (this.data.clickOne != mlId) {
          //   this.setData({
          //     clickOne: mlId,
          //     clickTwo: 0
          //   })
          // } else {
          //   this.setData({
          //     clickOne: 0,
          //     clickTwo: 0
          //   })
          // }
        }
      })
    } else {
      this.setData({
        clickOne: 0,
        clickTwo: 0
      })
    }
    

  },
  // 二级目录点击事件
  onSecond(e) {
    console.log(e);
    let mlId = e.currentTarget.dataset.mlid
    if(this.data.clickTwo != mlId || this.data.clickTwo == 0) {
      // this.getThird(mlId)
      
      request({
        url: '/zhongyisheji-applet/lookBook/public/bookPlanList',
        method: 'post',
        data: {
          pid: mlId,
          pageNum: '1',
          pageSize: '99',
          bookId: this.data.bookId
        }
      }).then(res => {
        console.log('三级目录',res);
        if(res.code == 200) {
          if(res.data.records.length == 0) {
            wx.showToast({
              title: '暂无子目录！',
              icon: 'none'
            })
          }
          this.setData({
            thirdList: res.data.records
          })

          this.setData({
            clickTwo: mlId
          })
          // if(this.data.clickTwo == 0) {
          //   this.setData({
          //     clickTwo: mlId
          //   })
          // } else if (this.data.clickTwo != mlId) {
          //   this.setData({
          //     clickTwo: mlId,
          //     clickTwo: 0
          //   })
          // } else {
          //   this.setData({
          //     // clickOne: 0,
          //     clickTwo: 0
          //   })
          // }
        }
      })
    } else {
        this.setData({
          // clickOne: 0,
          clickTwo: 0
        })
      }
    
    
  },
  // 二级目录   是视频时
  onPlayVideo(e) {
    console.log(e);
    let mlId = e.currentTarget.dataset.mlid
    // let isFree = e.currentTarget.dataset.free
    // if(isFree == 1) {
    //   wx.showToast({
    //     title: '购买后才能观看哦',
    //     icon: 'none',
    //     duration: 2000
    //   })
    //   return
    // }
    request({
      url: '/zhongyisheji-applet/lookBook/private/getbookPlanDetils',
      method: 'post',
      data: {
        bookId: this.data.bookId,
        catalogId: mlId
      }
    }).then(res => {
      console.log(res);
      // 1 不能看    2能看
      if(res.code == 200 && res.data.look == 2) {
        this.setData({
          videoUrl: res.data.bookCatalogContent.videoUrl,
          // showVideo: true,
        })
      } else if (res.code == 200 && res.data.look == 1) {
        wx.showToast({
          title: '购买后才能观看哦~',
          icon: 'none'
        }) 
      } else {
        wx.showToast({
          title: '播放失败',
          icon: 'none'
        })
      }
    })
  },
  // 三级目录点击事件
  onThird(e) {
    console.log(333);
    console.log(e);
    let id = e.currentTarget.dataset.tid
    wx.navigateTo({
      url: '/pages/index/goods/mulu-cont/mulu-cont?id=' + id + '&bookId='+this.data.bookId + '&pId='+ e.currentTarget.dataset.pid,
    })
    // wx.getStorage({
    //   key: 'userInfo',
    //   success: (res) => {
    //     console.log(res);
    //     wx.navigateTo({
    //       url: '/pages/index/goods/mulu-cont/mulu-cont?id=' + id + '&bookId='+this.data.bookId + '&pId='+ e.currentTarget.dataset.pid,
    //     })
    //   },
    //   fail: (res1) => {
    //   Dialog.confirm({
    //       title: '提示',
    //       confirmButtonText: '立即登录',
    //       message: '您暂未登录，请先登录才能查看详情',
    //     })
    //     .then(() => {
    //       wx.redirectTo({
    //         url: '/pages/login/login',
    //       })
    //     })
    //     .catch(() => {
    //       // on cancel
    //       Dialog.close()
    //     });
    //   }
    // })
  },

  // ===
  // 关闭视频弹窗
  onVideoClose() {
    this.setData({ showVideo: false });
  },
  // 跳转到内容
  goContent() {
    wx.navigateTo({
      url: '/pages/index/goods/mulu-cont/mulu-cont',
    })
  },
  // 购买书籍按钮
  goBuy() {
    console.log(111);
    wx.navigateTo({
      url: '/pages/index/goods/confirmOrder/confirmOrder?bookId=' + this.data.bookId,
    })
  },
  // 获取书的详情
  getBookDetail(bookId) {
    request({
      url: '/zhongyisheji-applet/lookBook/private/bookDetils',
      method: 'post',
      data: {
        bookId
      }
    }).then( res => {
      console.log('详情', res);
      if(res.code == 200) {
        let regex = new RegExp('<img', 'gi');
        res.data.book.bookContent= res.data.book.bookContent.replace(regex, `<img style="width: 100%;"`);
        this.setData({
          bookDetail: res.data
        })
      }
      
    })
  },
  // 获取一级目录
  getMuLu(bookId) {
    request({
      url: '/zhongyisheji-applet/lookBook/public/bookPlanList',
      method: 'post',
      data: {
        bookId,
        pid: '0',
        pageNum: 1,
        pageSize: '99'
      }
    }).then(res => {
      console.log('目录',res);
      if(res.code == 200) {
        this.setData({
          muLuList: res.data.records
        })
      }
    })
  },
  // 获取二级目录
  getSecond(one) {
    request({
      url: '/zhongyisheji-applet/lookBook/public/bookPlanList',
      method: 'post',
      data: {
        pid: one,
        pageNum: '1',
        pageSize: '99',
        bookId: this.data.bookId
      }
    }).then(res => {
      console.log('二级目录',res);
      if(res.code == 200) {
        if(res.data.records.length == 0) {
          wx.showToast({
            title: '暂无子目录！',
            icon: 'none'
          })
        }
        this.setData({
          secondList: res.data.records
        })
      }
    })
  },
  // 获取三级目录
  getThird(two) {
    request({
      url: '/zhongyisheji-applet/lookBook/public/bookPlanList',
      method: 'post',
      data: {
        pid: two,
        pageNum: '1',
        pageSize: '99',
        bookId: this.data.bookId
      }
    }).then(res => {
      console.log('三级目录',res);
      if(res.code == 200) {
        if(res.data.records.length == 0) {
          wx.showToast({
            title: '暂无子目录！',
            icon: 'none'
          })
        }
        this.setData({
          thirdList: res.data.records
        })
      }
    })
  },
  // 获取评论
  getPingLun(bookId) {
    request({
      url: '/zhongyisheji-applet/lookBook/public/getBookPingLun',
      method: 'post',
      data: {
        bookId: bookId,
        pageNum: '1',
        pageSize: '99'
      }
    }).then(res => {
      console.log('评论', res);
      this.setData({
        pingLunList: res.data.records
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    let bookId = options.bookId
    let that = this
    this.getBookDetail(bookId)
    this.getMuLu(bookId)
    this.getPingLun(bookId)
    this.setData({
      bookId: bookId
    })
    wx.createSelectorQuery().select('#tabs').boundingClientRect(function (rect) {
      console.log(rect.top);
      that.setData({
        scrollHeight: rect.top - that.data.statusBarHeight1
      })
    }).exec()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})